﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Wpf2.Models;
using Wpf2.ViewModels;
using WPFServices;

namespace Wpf2.Views
{
    /// <summary>
    /// Interaction logic for formQuanlydanhmuc.xaml
    /// </summary>
    public partial class FormManageCategory 
    {
        readonly NotifiableCollection<Catalogs> _catalogs;
        public FormManageCategory()
        {
            try
            {
                InitializeComponent();
                var listCatalogs = WPFServices.Services.Catalogs.GetListCatalogs().Select(x => new Catalogs
                {
                    IDCatalog = x.ID_CATALOG,
                    CatalogName = x.NAME_CATALOG,
                    CatalogNotes = x.NOTES_CATALOG
                });
                _catalogs = new NotifiableCollection<Catalogs>();
                foreach (Catalogs m in listCatalogs)
                {
                    _catalogs.Add(m);
                }
                DataContext = _catalogs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void DtgDanhmucLoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString(CultureInfo.InvariantCulture);
        }

        private void DtgDanhmucRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            try
            {
                if (e.EditAction != DataGridEditAction.Commit) return;
                var c = e.Row.DataContext as Catalogs;
                var action = _catalogs.Where(x => c != null && x.IDCatalog == c.IDCatalog).Select(x => x.IDCatalog).SingleOrDefault();
                if (action == 0)
                {
                    if (c != null)
                    {
                        var ca = new CATALOG
                                     {
                                         NAME_CATALOG = c.CatalogName,
                                         NOTES_CATALOG = c.CatalogNotes
                                     };

                        int idCa = WPFServices.Services.Catalogs.CreateCatalog(ca);
                        var firstOrDefault = _catalogs.FirstOrDefault(x => x.IDCatalog == 0);
                        if (firstOrDefault != null)
                            firstOrDefault.IDCatalog = idCa;
                    }
                }
                else
                {
                    if (c != null)
                    {
                        var currentC = WPFServices.Services.Catalogs.GetCatalogs(c.IDCatalog);
                        currentC.NAME_CATALOG = c.CatalogName;
                        currentC.NOTES_CATALOG = c.CatalogNotes;
                        WPFServices.Services.Catalogs.UpdateCatalog(currentC);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            try
            {
                var button = sender as Button;
                if (button == null) return;
                var p = button.DataContext as Catalogs;
                if (p == null || p.IDCatalog == 0) return;
                WPFServices.Services.Catalogs.DeleteCatalog(p.IDCatalog);
                _catalogs.Remove(p);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
